package test.ych.com.test

import java.io.File

//编译期常量
const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated"
class KotlinTest{

    // 可变变量
    var t1 = "0"
    var t4:Int? = null

    //只读变量
    val t2 = 0
    val t3:String = "1"

    //顶层变量 ： 函数以外的变量，包含可变与只读
    //lateinit : var的延迟初始化
    lateinit var t5:String
    //p对象第一次被调用时初始化
    val p:String by lazy{ "asd" }

    //枚举
    enum class Color{
        RED,
        GREND
    }

    //函数表达式
    fun sum(a:Int,b:Int):Int{
        return a+b
    }

    fun sum1(a:Int,b:Int) = a+b

    fun sum2(a:Int,b:Int):Unit{
        //字符串内嵌表达式
        println("sum a+b=${sum2(1,1)}")
    }

    // ? ：表示返回值可以为Null
    fun parseInt(str:String):Int? = null
    fun parseInt1(str:String = "a"):Int? = null

    //Any == Object   is/!is == of 类型判断
    fun getStringLength(obj:Any):Int?{
        if(obj is String){
            return obj.length
        }
        return null
    }

    //for 循环
    fun testfor(){
        val items = listOf(0,1,2,3,4,5)
        for(item in items){
//        for(item in items.indices){
            println(item)
        }
        for(z in 1..10){
            println(z)
        }
    }

    //while
    fun testWhile(){
        val items = listOf(0,1,2,3,4,5)
        var index = 0
        while (index < items.size){
            index++;
        }
    }

    //whem
    fun testWhen(obj:Any):String{
        return when(obj){
            1 ->"One"
            "Hello" -> "Two"
            is Long ->"is Long"
            else -> "Unknow"
        }
    }

    //range
    fun testRange(){
        val x = 10
        val y = 9
        //判断x是否在指定的区间
        if(x in 1..y+1){
            println("fits in range")
        }
        //循环1-5
        for(z in 1..5){
            println(z)
        }
        //数列迭代 1-10 步进为2
        for(z in 1..10 step 2){
            println(z)
        }
        //数列迭代 9-0 步进为3
        for(z in 9 downTo 0 step 3){
            println(z)
        }
    }

    // filter
    fun testFilter(){
        val fruits = listOf("banana","avocado","apple","kiwifruit")
        fruits.filter { it.startsWith("a") }
                .sortedBy { it }
                .map{it.toUpperCase()}
                .forEach { println(it) }
    }

    // list map
    fun testMap(){
        val fruits = listOf("banana","avocado","apple","kiwifruit")
        val maps = mapOf("a" to 1,"b" to 2)
        fruits.forEach { println(it) }
        for(map in maps){
            println(map.key+"  "+map.value)
        }
        var mapt = maps["a"]
    }

    //单列
    object Single{
        val name = "single"
    }

    //If not null
    fun testinf(){
        var file = File("Test").listFiles()
        println(file?.size)//表示返回值可以为null
    }

    //If not null and else
    fun testinfe(){
        val file = File("Test").listFiles();
        println(file?.size ?: "empty")
        file?.let { it[0] ?: "empty" }
    }

    //try catch
    fun testTry(){
       val tr = try {
           // do some thing
       }catch (e:Exception){
           e.printStackTrace()
       }finally {

       }
    }

    //with
    fun testWith(){
        val kt = KotlinTest();
        with(kt){
            sum(1,2)
            for(i in 1..4){
                sum1(1,2)
            }
            sum2(1,2)
        }
        var p = Point.fromPolar(1.0,2.0)
        var p1 = Point(1.0,2.0)
    }

//    class Point(val x:Double,y:Double){
    //伴生对象
      class Point constructor(val x:Double,y:Double){
        companion object {
            fun fromPolar(a:Double,b:Double) = Point(a,b)
        }
    }
    var po = Point.fromPolar(1.0,2.0)
    var po1 = Point(1.0,2.0)

    //作用域函数apply/with/run/also/let  it(可当参数传递)/this
    fun tesths(){
        var hs = "hs"
        hs.also { getStringLength(it) }
        hs.let{ getStringLength(it) }

        //结果是该上下文对象就用apply 、also  其他类型就用with,let,run
        //上下文对象可空使用apply,let,run,负责with,also
//        hs.apply {
//            text = ""
//            color = Red
//        }
    }

    //接口
    interface OnResults{
        fun bar()
        fun foo(){

        }
    }

    //扩展函数
    fun Int.str(index1:Int):Int{
        return index1
    }

    fun <T> T.str1(index1:T): String {
        var tmp = "index1 : ${index1}"
        return tmp;
    }

    fun testkz(){
        var str = Int.str1(10)
        println(str)
    }

    //数据类
    data class Person(val age:Int,val sex:String)

    //嵌套类与内部类 inner ：可以让内部类访问外部类的对象
    inner class Nested{
        fun foo() = t1
    }

    //属性委托
    interface Base {
        fun printMessage()
        fun printMessageLine()
    }

    class BaseImpl(val x: Int) : Base {
        override fun printMessage() { print(x) }
        override fun printMessageLine() { println(x) }
    }

    class Derived(b: Base) : Base by b {
        override fun printMessage() {
            print("abc")
        }
    }

    fun delegate() {
        val b = BaseImpl(10)
        Derived(b).printMessage()
        Derived(b).printMessageLine()
    }

}